<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;


class CollectController extends Controller
{
    //收藏
    public function index()
    {
    	
    	$data = DB::table('collect')
    		->leftJoin('product','product.product_id','=','collect.pro_id')
    		->where('collect.mem_id','=',session('member_id'))
    		->get();
    	// var_dump($data);
    	// dd($data);
    	return view('home/main/collect',['data'=>$data]);
    	
	}


	//添加收藏
	public function add(Request $request)
	{
		$data = $request -> except('_token');
		$rre = DB::table('collect') -> where('pro_id',$data['proid']) -> where('mem_id',session('member_id')) -> get();
		if(!empty($rre))
		{
			$a = 0;
			return response() -> json($a);
		}
		
		$res = DB::table('collect') -> where('pro_id',$data['proid']) -> insert(['mem_id'=>session('member_id'),'ctime'=>time(),'pro_id'=>$data['proid']]);
		if($res)
		{
			$a = 1;
			return response() -> json($a);
		}
		// return response() -> json($data['proid']);
		
	}

	//删除收藏
	public function del($id)
	{
		$res = DB::table('collect') -> where('collect_id',$id) -> delete();
		if($res)
		{
			return redirect('/collect');
		}else
		{
			return back();
		}
	}

    
}
